/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utn.tsb.tej.front_end.Maps;

import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.core.QRectF;
import com.trolltech.qt.core.QTimer;
import com.trolltech.qt.gui.QGraphicsItem;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QStyleOptionGraphicsItem;
import com.trolltech.qt.gui.QWidget;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class Arrow extends QGraphicsItem{

    //Statics Arrows Types
    public static final int BLUE_ARROW = 0;
    public static final int RED_ARROW = 1;

    private QPixmap image;
    private int x, y;
    private int maxArrowMovement, arrowCounter, arrowSpeed;
    private QRectF rect;
    private QTimer timer;

    public Arrow (int x, int y, int arrowType)
    {
        super();
        
        //File Separator
        String osBar = java.io.File.separator;

        String fileName="";
        switch(arrowType)
            {
            case BLUE_ARROW:
                fileName = "Arrow_Blue.png";
                break;
            case RED_ARROW:
                fileName = "Arrow_Red.png";
                break;
            default:
                fileName = "Arrow_Blue.png";
                break;
        }

        try{
        image = new QPixmap(getClass().getResource(osBar + "images"+
                osBar + fileName).getPath());
        }
        catch (NullPointerException e)
        {
            System.out.println("EXEC: Arrow image not found!");
        }

        this.x = x;
        this.y = y;
        this.setPos(x, y);
        
        maxArrowMovement = 10;
        arrowCounter = 0;
        arrowSpeed = -1;

        arrowAnimation();

    }

    public Arrow(int x, int y)
    {
        this(x, y, BLUE_ARROW);
    }

    public Arrow ()
    {
        this (0,0, BLUE_ARROW);
    }
    private void updateArrow()
    {
        rect = new QRectF(-image.width()/2, -image.height()+maxArrowMovement,
                image.width(), image.height()+maxArrowMovement);
    }

    public QRectF getRectF()
    {
        return rect;
    }

    private void arrowAnimation()
    {
        timer = new QTimer();
        timer.timeout.connect(this, "on_bArrowTimer_timeout()");
        timer.start(100);
    }

    private void on_bArrowTimer_timeout()
    {
        QPointF pos = pos();

        if (arrowCounter == maxArrowMovement)
        {
            arrowCounter = 0;
            arrowSpeed = arrowSpeed * -1;
        }

        pos.setY(pos.y() + arrowSpeed);
        setPos(pos);
        arrowCounter++;
    }

    @Override
    public QRectF boundingRect() {
        double penWidth = 1;
        //return rect;
        return new QRectF(-14 - penWidth / 2, -24 - penWidth / 2,
                       28 + penWidth, 28 + penWidth);
    }

    @Override
    public void paint(QPainter qp, QStyleOptionGraphicsItem qsogi, QWidget qw) {
        qp.drawPixmap(-14, -28, image);
    }

}
